package com.hc.lookSys.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hc.lookSys.util.MyCons;
import com.hc.lookSys.util.MyCos;

public class BookInfoDao {
	public List<Map<String, Object>> findAll() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String sql = "SELECT bi.book_code,bt.type_name,bi.book_name,bi.book_auther,bi.public_press,bi.is_borrow from booke_info as bi INNER JOIN book_type as bt on bi.book_id=bt.id INNER JOIN users as u on u.user_id=bt.id";
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			Class.forName(MyCons.DRIVER);
			connection = DriverManager.getConnection(MyCons.URL, MyCos.USER,
					MyCos.PASSWORD);
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("bookCode", rs.getInt("book_code"));
				map.put("typeName", rs.getString("type_name"));
				map.put("bookName", rs.getString("book_name"));
				map.put("bookAuther", rs.getString("book_auther"));
				map.put("publicPress", rs.getString("public_press"));
				map.put("isBorrow", rs.getInt("is_borrow"));
				list.add(map);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != rs) {
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (null != rs) {
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (null != rs) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return list;
	}

}